package com.ruoyi.article.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.article.domain.ZArticle;
import com.ruoyi.article.domain.ZArticleReview;
import com.ruoyi.article.domain.vo.LikeAndCollect;
import com.ruoyi.article.service.IZArticleReviewService;
import com.ruoyi.article.service.IZArticleService;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.article.mapper.ZPraiseLogMapper;
import com.ruoyi.article.domain.ZPraiseLog;
import com.ruoyi.article.service.IZPraiseLogService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户评论点赞记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-09
 */
@Service
public class ZPraiseLogServiceImpl extends ServiceImpl<ZPraiseLogMapper,ZPraiseLog> implements IZPraiseLogService
{
    @Autowired
    private ZPraiseLogMapper zPraiseLogMapper;

    @Autowired
    private IZArticleService zArticleService;
    @Autowired
    private IZArticleReviewService zArticleReviewService;
    @Autowired
    private ISysUserService sysUserService;

    @Transactional
    @Override
    public int insertReviewLog(Long id,Long contentId,Integer status) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        String username = loginUser.getUsername();
        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getReviewId,id)
                .eq(ZPraiseLog::getUserId,userId);
        //1代表评论已经被点赞了，现在是需要给点赞取消掉
        if (status == 1){
            //删除ZPraiseLog记录和ArticleReview的getLikeNum-1

            //更新评论点赞数量
            ZArticleReview byId = zArticleReviewService.getById(id);
            byId.setLikeNum(byId.getLikeNum()-1);
            zArticleReviewService.updateById(byId);


            zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getStatus,2);
            ZPraiseLog zPraiseLog = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);
            if (ObjectUtils.isNull(zPraiseLog)){
                throw new ServiceException("当前用户评论日志数据为空");
            }



            return zPraiseLogMapper.deleteById(zPraiseLog);
        }
        //先查看日志中是不是存在记录，（同一用户，同一评论只能点赞一次）

        int insert = -1;
        Long l = zPraiseLogMapper.selectCount(zPraiseLogLambdaQueryWrapper);

        if (l == 0L){
            //数据库中不存在 可以新增记录
            //新增用户给评论点赞记录
            ZPraiseLog zPraiseLog = new ZPraiseLog();
            zPraiseLog.setReviewId(id);
            zPraiseLog.setArticleId(contentId);
            zPraiseLog.setUserId(userId);
            zPraiseLog.setUserName(username);
            //2代表评论的点赞
            zPraiseLog.setStatus("2");
            insert = zPraiseLogMapper.insert(zPraiseLog);
            //更新评论点赞数量
            ZArticleReview byId = zArticleReviewService.getById(id);
            byId.setLikeNum(byId.getLikeNum()+1);
            zArticleReviewService.updateById(byId);
        }else {
            throw new ServiceException("不可重复点赞");
        }
        return insert;
    }

    @Override
    public TableDataInfo selectRevieLikeList(Long contentId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,userId)
                .eq(ZPraiseLog::getArticleId,contentId);
        //得到当前用户，当前文章的所有评论点赞日志记录
        List<ZPraiseLog> zPraiseLogs = zPraiseLogMapper.selectList(zPraiseLogLambdaQueryWrapper);
        List<Long> reviewIds = zPraiseLogs.stream().map(ZPraiseLog::getReviewId).collect(Collectors.toList());
        List<ZArticleReview> zArticleReviews = zArticleReviewService.listByIds(reviewIds);

        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setMsg("查询成功");
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setRows(zArticleReviews);
        return dataInfo;
    }

    /**
     * 查询用户评论点赞记录
     * 
     * @param id 用户评论点赞记录主键
     * @return 用户评论点赞记录
     */
    @Override
    public ZPraiseLog selectZPraiseLogById(Long id)
    {
        return zPraiseLogMapper.selectZPraiseLogById(id);
    }

    /**
     * 查询用户评论点赞记录列表
     * 
     * @param zPraiseLog 用户评论点赞记录
     * @return 用户评论点赞记录
     */
    @Override
    public List<ZPraiseLog> selectZPraiseLogList(ZPraiseLog zPraiseLog)
    {
        return zPraiseLogMapper.selectZPraiseLogList(zPraiseLog);
    }

    /**
     * 新增用户评论点赞记录
     * 
     * @param zPraiseLog 用户评论点赞记录
     * @return 结果
     */
    @Override
    public int insertZPraiseLog(ZPraiseLog zPraiseLog)
    {
        return zPraiseLogMapper.insertZPraiseLog(zPraiseLog);
    }

    /**
     * 修改用户评论点赞记录
     * 
     * @param zPraiseLog 用户评论点赞记录
     * @return 结果
     */
    @Override
    public int updateZPraiseLog(ZPraiseLog zPraiseLog)
    {
        return zPraiseLogMapper.updateZPraiseLog(zPraiseLog);
    }

    /**
     * 批量删除用户评论点赞记录
     * 
     * @param ids 需要删除的用户评论点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteZPraiseLogByIds(Long[] ids)
    {
        return zPraiseLogMapper.deleteZPraiseLogByIds(ids);
    }

    /**
     * 删除用户评论点赞记录信息
     * 
     * @param id 用户评论点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteZPraiseLogById(Long id)
    {
        return zPraiseLogMapper.deleteZPraiseLogById(id);
    }

    @Override
    public int insertReviewLog(Long articleId, String status) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        String username = loginUser.getUsername();

        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,userId)
                .eq(ZPraiseLog::getArticleId,articleId)
                .eq(ZPraiseLog::getStatus,"1");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);

        //收藏文章
        if ("true".equals(status)){
            //判断是否已经收藏
            if (ObjectUtils.isNotNull(zPraiseLog1)){
                throw new ServiceException("不能重复收藏");
            }
            ZPraiseLog zPraiseLog = new ZPraiseLog();
            zPraiseLog.setStatus("1");
            zPraiseLog.setUserId(userId);
            zPraiseLog.setUserName(username);
            zPraiseLog.setArticleId(articleId);
            zPraiseLogMapper.insert(zPraiseLog);
        }

        //取消收藏
        if ("false".equals(status)){
            if (ObjectUtils.isNull(zPraiseLog1)){
                throw new ServiceException("数据错误,无数据");
            }
            zPraiseLogMapper.deleteById(zPraiseLog1);
        }
        return 1;
    }

    @Override
    public Boolean getIsCollect(Long articleId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        //查询当前用户是否收藏当前文章，文章收藏代表的是1
        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,userId)
                .eq(ZPraiseLog::getArticleId,articleId)
                .eq(ZPraiseLog::getStatus,"1");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);
        //数据库没数据，说明文章没有被收藏
        if (ObjectUtils.isNull(zPraiseLog1)){
            return false;
        }
        return true;
    }

    @Override
    public int insertReviewLogArticleLike(Long articleId, String status) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        String username = loginUser.getUsername();
        ZArticle article = zArticleService.getById(articleId);

        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,userId)
                .eq(ZPraiseLog::getArticleId,articleId)
                //文章点赞
                .eq(ZPraiseLog::getStatus,"0");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);

        //点赞文章
        if ("true".equals(status)){
            //判断是否已经点过赞
            if (ObjectUtils.isNotNull(zPraiseLog1)){
                throw new ServiceException("不能重复点赞");
            }
            ZPraiseLog zPraiseLog = new ZPraiseLog();
            //文章点赞
            zPraiseLog.setStatus("0");
            zPraiseLog.setUserId(userId);
            zPraiseLog.setUserName(username);
            zPraiseLog.setArticleId(articleId);
            zPraiseLogMapper.insert(zPraiseLog);
            //给当前文章点赞数量加1

            article.setLikeNum(article.getLikeNum()+1);
            zArticleService.updateById(article);
        }

        //取消收藏
        if ("false".equals(status)){
            if (ObjectUtils.isNull(zPraiseLog1)){
                throw new ServiceException("数据错误,无数据");
            }
            zPraiseLogMapper.deleteById(zPraiseLog1);
            //文章点赞数-1
            article.setLikeNum(article.getLikeNum()-1);
            zArticleService.updateById(article);
        }
        return 1;
    }

    @Override
    public Boolean getIsLoveStar(Long articleId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        //查询当前用户是否收藏当前文章，文章收藏代表的是1
        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,userId)
                .eq(ZPraiseLog::getArticleId,articleId)
                //文章点赞
                .eq(ZPraiseLog::getStatus,"0");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);
        //数据库没数据，说明文章没有被收藏
        if (ObjectUtils.isNull(zPraiseLog1)){
            return false;
        }
        return true;
    }

    @Override
    public Boolean getIsIsLike(LikeAndCollect likeAndCollect) {


        //查询当前用户是否收藏当前文章，文章收藏代表的是1
        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,likeAndCollect.getUserId())
                .eq(ZPraiseLog::getArticleId,likeAndCollect.getArticleId())
                //文章点赞
                .eq(ZPraiseLog::getStatus,"0");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);
        //数据库没数据，说明文章没有被收藏
        if (ObjectUtils.isNull(zPraiseLog1)){
            return false;
        }
        return true;
    }

    @Override
    public int insertReviewArticleLike(Long articleId, String status, Long userId) {
        SysUser sysUser = sysUserService.selectUserById(userId);
        String username = sysUser.getUserName();
        ZArticle article = zArticleService.getById(articleId);

        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,userId)
                .eq(ZPraiseLog::getArticleId,articleId)
                //文章点赞
                .eq(ZPraiseLog::getStatus,"0");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);

        //点赞文章
        if ("true".equals(status)){
            //判断是否已经点过赞
            if (ObjectUtils.isNotNull(zPraiseLog1)){
                throw new ServiceException("不能重复点赞");
            }
            ZPraiseLog zPraiseLog = new ZPraiseLog();
            //文章点赞
            zPraiseLog.setStatus("0");
            zPraiseLog.setUserId(userId);
            zPraiseLog.setUserName(username);
            zPraiseLog.setArticleId(articleId);
            zPraiseLogMapper.insert(zPraiseLog);
            //给当前文章点赞数量加1

            article.setLikeNum(article.getLikeNum()+1);
            zArticleService.updateById(article);
        }

        //取消收藏
        if ("false".equals(status)){
            if (ObjectUtils.isNull(zPraiseLog1)){
                throw new ServiceException("数据错误,无数据");
            }
            zPraiseLogMapper.deleteById(zPraiseLog1);
            //文章点赞数-1
            article.setLikeNum(article.getLikeNum()-1);
            zArticleService.updateById(article);
        }
        return 1;
    }

    @Override
    public Boolean getIsArticleCollect(LikeAndCollect likeAndCollect) {

        //查询当前用户是否收藏当前文章，文章收藏代表的是1
        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,likeAndCollect.getUserId())
                .eq(ZPraiseLog::getArticleId,likeAndCollect.getArticleId())
                .eq(ZPraiseLog::getStatus,"1");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);
        //数据库没数据，说明文章没有被收藏
        if (ObjectUtils.isNull(zPraiseLog1)){
            return false;
        }
        return true;
    }

    @Override
    public int insertArticleReviewLog(Long articleId, String status, Long userId) {
        SysUser sysUser = sysUserService.selectUserById(userId);
        String username = sysUser.getUserName();

        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getUserId,userId)
                .eq(ZPraiseLog::getArticleId,articleId)
                .eq(ZPraiseLog::getStatus,"1");
        ZPraiseLog zPraiseLog1 = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);

        //收藏文章
        if ("true".equals(status)){
            //判断是否已经收藏
            if (ObjectUtils.isNotNull(zPraiseLog1)){
                throw new ServiceException("不能重复收藏");
            }
            ZPraiseLog zPraiseLog = new ZPraiseLog();
            zPraiseLog.setStatus("1");
            zPraiseLog.setUserId(userId);
            zPraiseLog.setUserName(username);
            zPraiseLog.setArticleId(articleId);
            zPraiseLogMapper.insert(zPraiseLog);
        }

        //取消收藏
        if ("false".equals(status)){
            if (ObjectUtils.isNull(zPraiseLog1)){
                throw new ServiceException("数据错误,无数据");
            }
            zPraiseLogMapper.deleteById(zPraiseLog1);
        }
        return 1;
    }

    @Override
    public int insertReviewLikeLog(Long id, Long contentId, Integer status, Long userId,String username) {



        LambdaQueryWrapper<ZPraiseLog> zPraiseLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getReviewId,id)
                .eq(ZPraiseLog::getUserId,userId);
        //1代表评论已经被点赞了，现在是需要给点赞取消掉
        if (status == 1){
            //删除ZPraiseLog记录和ArticleReview的getLikeNum-1

            //更新评论点赞数量
            ZArticleReview byId = zArticleReviewService.getById(id);
            byId.setLikeNum(byId.getLikeNum()-1);
            zArticleReviewService.updateById(byId);


            zPraiseLogLambdaQueryWrapper.eq(ZPraiseLog::getStatus,2);
            ZPraiseLog zPraiseLog = zPraiseLogMapper.selectOne(zPraiseLogLambdaQueryWrapper);
            if (ObjectUtils.isNull(zPraiseLog)){
                throw new ServiceException("当前用户评论日志数据为空");
            }



            return zPraiseLogMapper.deleteById(zPraiseLog);
        }
        //先查看日志中是不是存在记录，（同一用户，同一评论只能点赞一次）

        int insert = -1;
        Long l = zPraiseLogMapper.selectCount(zPraiseLogLambdaQueryWrapper);

        if (l == 0L){
            //数据库中不存在 可以新增记录
            //新增用户给评论点赞记录
            ZPraiseLog zPraiseLog = new ZPraiseLog();
            zPraiseLog.setReviewId(id);
            zPraiseLog.setArticleId(contentId);
            zPraiseLog.setUserId(userId);
            zPraiseLog.setUserName(username);
            //2代表评论的点赞
            zPraiseLog.setStatus("2");
            insert = zPraiseLogMapper.insert(zPraiseLog);
            //更新评论点赞数量
            ZArticleReview byId = zArticleReviewService.getById(id);
            byId.setLikeNum(byId.getLikeNum()+1);
            zArticleReviewService.updateById(byId);
        }else {
            throw new ServiceException("不可重复点赞");
        }
        return insert;
    }
}
